<?php
	
	$mktgTbl = "mktg_announcesContactsData";
	// First letter of each word, to upper
	$name = ucwords(strtolower($name));
	// email should be full lowercase
	$email = strtolower($email);
	// correct phone strings merge
	$array = explode(" ", $phone);
	$phone_car = $array[0];
	$phone_num = $array[1];
	$phonePhone = $phone_car.$phone_num;
	
	
if(!empty($name) && !empty($email) && !empty($ad_id)) {
		// Lets get our current AD parent category ID
		$s1 = "SELECT ad_cat_id FROM $ads_tbl WHERE ad_id = $ad_id";
		$exec_s1 = q($s1);
			$row_s1 = mysql_fetch_array($exec_s1);
			$s1_ad_cat_id = $row_s1["ad_cat_id"];
		
		// Prevent getting grandFather Cat ids too easly
		if($s1_ad_cat_id != '1000' || $s1_ad_cat_id != '1001' || $s1_ad_cat_id != '1002') {
			$s2 = "SELECT cat_fatherid FROM $cat_tbl WHERE cat_id = $s1_ad_cat_id";
			$exec_s2 = q($s2);
				$row_s2 = mysql_fetch_array($exec_s2);
				$s2_cat_fatherid = $row_s2["cat_fatherid"];
		}
		
		// Validate if this dude is already on the mktgContacts tracking list
		// If he does, we do nothing, we only store unique contacts per ad_id <=> email
		$chk_sql = "SELECT userId FROM $mktgTbl WHERE email = '$email' AND ad_id = $ad_id";
		$exec_chk = q($chk_sql);
			$chk = mysql_num_rows($exec_chk);
		if($chk == 0) {
			$sql_srt = "INSERT INTO $mktgTbl (userId, name, email, phone, ad_id, ad_cat_id, ad_fatherCat_id) 
										VALUES ('','$name','$email','$phonePhone','$ad_id','$s1_ad_cat_id','$s2_cat_fatherid')";
			$exec_sp = q($sql_srt);
		}
}
	
?>
